package cn.hiapi.user.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.basic.service.BasicService;
import cn.hiapi.basic.token.Token;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shop.entity.shop.Shop;
import cn.hiapi.shop.service.shop.ShopService;
import cn.hiapi.shop.vo.ShopBase;
import cn.hiapi.shopkeeper.entity.activity.ActivityCodeRecord;
import cn.hiapi.shopkeeper.entity.order.ActivityCardOrder;
import cn.hiapi.shopkeeper.entity.order.ActivityOrder;
import cn.hiapi.shopkeeper.enums.OrderStatus;
import cn.hiapi.shopkeeper.service.activity.ActivityCodeRecordService;
import cn.hiapi.shopkeeper.service.order.ActivityCardOrderService;
import cn.hiapi.user.query.ActivityCardOrderQuery;
import cn.hiapi.user.vo.ActivityCardOrderVo;
import cn.hiapi.utils.JsonUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2023/1/2 15:20
 */
@RestController
@RequestMapping("/user/activity-card")
public class OrderCardController extends BasicQueryController<ActivityCardOrder, Long, ActivityCardOrderVo, ActivityCardOrderQuery> {
    protected OrderCardController(ActivityCardOrderService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
    }

    @ApiOperation(value = "核销码列表", notes = "核销码列表", nickname = "AdinZ")
    @GetMapping("/card-list")
    public ResponseEntity cardList(@RequestParam("cardId") long id) {
        ActivityCardOrder order = this.dispatchContext.getServiceOne(ActivityCardOrderService.class).get(id);
        if (order == null || TokenGet.get().getFid() != order.getBuyerId()) return toError("订单不存在");
        if (order.getStatus() != 0) return toError("订单还没支付");
        if (order.getUseEnd() < System.currentTimeMillis()) return toError("卡包已过期");
        List<ActivityCodeRecord> list = this.dispatchContext.getServiceOne(ActivityCodeRecordService.class).findList(new QueryWrapper().eq("oid", order.getOid()));
        List<JSONObject> collect = list.stream().map(e -> JsonUtils.builder()
                .put("code", e.getCode())
                .put("status", e.getStatus())
                .put("updated", e.getUpdated())
                .build()).collect(Collectors.toList());
        return toSuccess(collect);
    }

    @Override
    public ActivityCardOrderQuery setQuery(ActivityCardOrderQuery query) {
        Token<Object> token = TokenGet.get();
        query.setBuyerId(token.getFid());
        return super.setQuery(query);
    }

    @Override
    public List<ActivityCardOrderVo> toListVo(List<ActivityCardOrder> list, String[] fields) {
        return list.stream().map(e -> this.toData(e, fields)).collect(Collectors.toList());
    }

    @Override
    public ActivityCardOrderVo toData(ActivityCardOrder data, String[] fields) {
        ActivityCardOrderVo vo = data.copy(new ActivityCardOrderVo());
        if(vo.getStatus()==0){
            if(vo.getUseEnd()<System.currentTimeMillis()){
                vo.setStatus(2);
            }
        }
        Shop shop = this.dispatchContext.getServiceOne(ShopService.class).get(data.getShopId());
        if(shop!=null) vo.setShop(shop.copy(new ShopBase()));
        return vo;
    }
}
